<template>
  <div class="history-container">
    <div class="top">
      <div class="fanhui-btn" @click="handleRoute">
        <i class="el-icon-arrow-left"></i>
        <span>返回</span>
      </div>
      <div class="lujing">
        <span>当前位置：系统维护/换流站维护/状态评估维护</span>
      </div>
    </div>
    <div class="content">
      <el-form class="form-search" inline>
        <label class="btn-nav-bg stripe-btn" @click="handleAdd">新 增</label>
      </el-form>
      <page-table
        :tableData="tableData"
        :tableLabel="colSData"
        :showSelection="false"
        :pager="pager"
        showIndex
        :table-height="tableHeight"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
        @handleButton="handleButton(arguments)"
        @handleLink="handleLink(arguments)"
      ></page-table>
    </div>
    <pub-dialog
      :title="dialogTitle + '换流阀'"
      type="big"
      top="15vh"
      :sureBtnLoading="loading"
      :dialogVisible="dialogVisible"
      dialogVisableRef="dialogSateVisible"
      @switchVisable="switchVisable"
      @dialogSubmit="dialogSubmit"
      @dialogCancel="dialogCancel"
      btnType="3"
    >
      <el-form
        class="form-search"
        label-width="120px"
        :rules="rules"
        ref="formData"
        :model="formData"
      >
        <el-form-item label="换流阀名称" prop="valve">
          <el-input placeholder="请输入" v-model="formData.valve"></el-input>
        </el-form-item>
        <el-form-item label="设备型号" prop="unitType">
          <el-input placeholder="请输入" v-model="formData.unitType"></el-input>
        </el-form-item>
        <el-form-item label="电压等级" prop="voltageLevel">
          <el-input
            placeholder="请输入"
            v-model="formData.voltageLevel"
          ></el-input>
        </el-form-item>
        <el-form-item label="生产厂家" prop="vender">
          <el-input placeholder="请输入" v-model="formData.vender"></el-input>
        </el-form-item>
        <el-form-item label="投运日期" prop="putDate">
          <el-date-picker
            v-model="formData.putDate"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            placeholder="请选择投运时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
    </pub-dialog>
    <pub-dialog
      title="设置报点ID"
      type="default"
      top="15vh"
      :sureBtnLoading="loading"
      :dialogVisible="dialogMeasureVisible"
      dialogVisableRef="dialogSateVisible"
      @switchVisable="switchVisable"
      @dialogSubmit="dialogMeasureSubmit"
      @dialogCancel="dialogMeasureCancel"
      btnType="3"
    >
      <el-form
        class="form-search"
        label-width="200px"
        :rules="rules"
        ref="formRef"
        :model="form"
      >
        <p
          style="text-align: center; color: #ccc; font-size: 18px"
          v-if="!measureList.length"
        >
          暂无数据
        </p>
        <el-row v-else :gutter="24">
          <el-col :span="8" v-for="(value, label, index) in form" :key="index">
            <el-form-item
              :label="label"
              :prop="label"
              :rules="[{ required: true, message: '请输入', trigger: 'blur' }]"
            >
              <el-input clearable placeholder="请输入" v-model="form[label]" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </pub-dialog>
  </div>
</template>
<script>
import PageTable from "@/components/PageTable.vue";
import {
  xjValveAdd,
  xjValveUpdate,
  xjValveDelete,
  xjValveDetail,
  xjMeasureDeviceDetail,
  xjMeasureDeviceAdd,
} from "@/api/pointMaintain";
import { getXjValueList } from "@/api/earlyWarning";
import pubDialog from "@/components/pubDialog.vue";

export default {
  name: "Manage",
  props: {
    siteId: {
      type: String,
      default: "",
    },
  },
  components: {
    pubDialog,
    PageTable,
  },
  data() {
    return {
      tableData: [],
      colSData: [
        { label: "换流阀名称", param: "valve" },
        { label: "设备型号", param: "unitType", link: true },
        { label: "电压等级", param: "voltageLevel" },
        { label: "生产厂家", param: "vender" },
        { label: "投运日期", param: "putDate" },
        { label: "换流阀模型", param: "modal" },
        {
          label: "操作",
          param: "operate",
          width: 450,
          operate: [
            {
              label: "进入管理",
              click: "manage",
              type: "text",
            },
            {
              label: "测点参数",
              click: "param",
              type: "text",
            },
            {
              label: "编辑",
              click: "edit",
              type: "text",
            },
            {
              label: "删除",
              click: "remove",
              type: "text",
            },
          ],
        },
      ],
      pager: {
        pageSize: 10, // 每页数据量
        currentPage: 1, // 分页  当前页
        total: 0, //列表总数
      },
      tableHeight: 400,

      dialogVisible: false,
      dialogTitle: "新增",
      formData: {
        valve: "",
        unitType: "",
        voltageLevel: "",
        vender: "",
        putDate: "",
      },
      rules: {},

      dialogMeasureVisible: false,
      loading: false,
      measureList: [],
      deviceId: null,
      form: {},
    };
  },
  created() {
    this.calcHeight();
    this.getList();
  },
  methods: {
    calcHeight() {
      this.tableHeight = window.innerHeight - 150;
    },
    handleSizeChange(val) {
      this.pager.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pager.currentPage = val;
      this.getList();
    },
    // 新增
    handleAdd() {
      this.dialogVisible = true;
      this.dialogTitle = "新增";
      this.formData = {
        valve: "",
        unitType: "",
        voltageLevel: "",
        vender: "",
        putDate: "",
      };
    },
    // 列表行操作
    handleLink(method, row, index) {
      this.$router.push({
        path: "/pointMaintain/manage/managePage",
        query: {
          valveId: method[1].id,
          valve: method[1].valve,
          siteName: this.$route.query.siteName,
        },
      });
    },
    // 操作
    handleButton(method, row, index) {
      if (method[0] === "remove") {
        xjValveDelete({ id: method[1].id }).then((res) => {
          if (res.code == 20000) {
            this.$message.success("删除成功");
            this.getList();
          } else {
            this.$message.error(res.msg);
          }
        });
      } else if (method[0] === "edit") {
        this.dialogVisible = true;
        this.dialogTitle = "编辑";
        xjValveDetail({ id: method[1].id }).then((res) => {
          if (res.code == 20000) {
            this.formData = res.data;
          }
        });
      } else if (method[0] === "manage") {
        this.$router.push({
          path: "/pointMaintain/manage/managePage",
          query: {
            valveId: method[1].id,
            valve: method[1].valve,
            siteName: this.$route.query.siteName,
          },
        });
      } else {
        this.dialogMeasureVisible = true;
        // let arr = [
        //   {
        //     id: 16,
        //     measureId: 18,
        //     paramName: "test1",
        //   },
        //   {
        //     id: 17,
        //     measureId: 19,
        //     paramName: "test2",
        //   },
        // ];
        // arr.forEach((item) => {
        //   this.$set(this.form, item.paramName, item.uniqueId);
        // });
        //
        // this.measureList = arr;

        this.deviceId = method[1].id;
        let params = {
          deviceId: method[1].id, // 设备id
          deviceType: 0, // 设备类型 子模块1 换流阀0
        };
        xjMeasureDeviceDetail(params).then((res) => {
          if (res.code == 20000) {
            res.data.forEach((item) => {
              this.$set(this.form, item.paramName, item.uniqueId);
            });
            this.measureList = res.data;
          }
        });
      }
    },
    // 查询列表
    async getList() {
      let params = {
        siteId: this.$route.query.siteId,
      };
      let result = await getXjValueList(params);
      if (result.code == 20000) {
        this.tableData = result.data;
      }
    },

    switchVisable(visable, ref) {
      this[ref] = visable;
    },
    dialogCancel() {
      this.dialogVisible = false;
    },

    dialogSubmit() {
      this.loading = true;
      let params = { ...this.formData };
      params.siteId = this.$route.query.siteId;
      let API = params.id ? xjValveUpdate : xjValveAdd;
      API(params)
        .then((res) => {
          if (res.code == 20000) {
            this.dialogVisible = false;
            this.getList();
            this.$message.success("保存成功");
          }
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
          return err;
        });
    },

    // 报点参数取消
    dialogMeasureCancel() {
      this.dialogMeasureVisible = false;
    },
    // 报点参数新增、编辑保存
    dialogMeasureSubmit() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.measureList.forEach((item) => {
            item.uniqueId = this.form[item.paramName];
            item.deviceId = this.deviceId;
          });
          let params = [...this.measureList];
          this.loading = true;
          xjMeasureDeviceAdd(params)
            .then((res) => {
              if (res.code == 20000) {
                this.dialogVisible = false;
                this.getList();
                this.dialogMeasureVisible = false;
                this.$message.success("保存成功");
              }
              this.loading = false;
            })
            .catch((err) => {
              this.loading = false;
              return err;
            });
        } else {
          return false;
        }
      });
    },

    handleRoute() {
      this.$router.go(-1);
    },
  },
};
</script>
<style scoped lang="less">
::v-deep .form-search .el-form-item {
  margin-bottom: 22px;
}
.history-container {
  height: 100%;
  .top {
    height: 60px;
    display: flex;
    align-items: center;
    color: #fff;
    .fanhui-btn {
      cursor: pointer;
      span {
        margin-left: 5px;
      }
    }
    .lujing {
      margin-left: 50px;
    }
  }
  ::v-deep .content {
    height: calc(100% - 60px);
    background-color: transparent;
    border: 2px solid #40486a;
    padding: 15px;
    box-sizing: border-box;
  }
  .form-search {
    margin-bottom: 16px;
  }
}
</style>
